package com.forezp.web;

import com.forezp.config.Constant;
import com.forezp.entity.DataSource;
import com.forezp.entity.Task;
import com.forezp.entity.TaskNode;
import com.forezp.service.DataSourceService;
import com.forezp.service.TaskNodeService;
import com.forezp.service.TaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * springboot-jdbc-DataSourceController
 *
 * @author <a href="2428922347@qq.com">Zhu mingming</a>
 * @info
 * @since 2018/3/16 14:16
 */
@Controller
public class TaskController {

    @Autowired
    private TaskService taskService;

    @Autowired
    private TaskNodeService taskNodeService;

    @RequestMapping("/task")
    public String task(Model model, Integer taskId) {
        List<Task> all = taskService.getAll();
        model.addAttribute("task", all);
        if (taskId != null) {
            Task task = taskService.findById(taskId);
            model.addAttribute("targetTask", task);
        }
        return "task";
    }


    @RequestMapping("/task/del/{taskId}")
    public String delTask(@PathVariable("taskId") Integer id) {
        taskService.del(id);
        return "forward:/" + Constant.prefix + "/task";
    }

    @RequestMapping("/task/add")
    public String addTask(Task task) {
        taskService.save(task);
        return "forward:/" + Constant.prefix + "/task";
    }


    @RequestMapping("/task/detail/{taskId}")
    public String detail(@PathVariable("taskId") Integer taskId, Model model, Integer taskNodeId) {
        List<TaskNode> taskNodes = taskNodeService.findByTaskIdOrderByOrderDesc(taskId);
        Task task = taskService.findById(taskId);
        model.addAttribute("task", task);
        model.addAttribute("taskNode", taskNodes);
        if (taskNodeId != null) {
            TaskNode taskNode = taskNodeService.findById(taskNodeId);
            model.addAttribute("targetTaskNode", taskNode);
        }
        return "taskDetail";
    }


    @RequestMapping("/task/taskNode/add")
    public String addTaskNode(TaskNode taskNode) {
        taskNodeService.save(taskNode);
        return "forward:/" + Constant.prefix + "/task/detail/" + taskNode.getTaskId();
    }


    @RequestMapping("/task/taskNode/del/{taskNodeId}")
    public String addTaskNode(@PathVariable("taskNodeId") Integer taskNodeId) {
        TaskNode taskNode = taskNodeService.findById(taskNodeId);
        taskNodeService.del(taskNodeId);
        return "forward:/" + Constant.prefix + "/task/detail/" + taskNode.getTaskId();
    }

    @RequestMapping("**")
    public String to(HttpServletRequest httpServletRequest) {
        return httpServletRequest.getRequestURI().substring(1);
    }


}
